package com.action;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.City;
import com.pojo.Demo;
import com.pojo.Department;
import com.pojo.Page;
import com.pojo.School;
import com.pojo.Test;

@Controller
@RequestMapping("/")
public class TestController {

    @GetMapping("j/index")
    public String jIndex(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
        return "jsp/index";
    }
    
    @PostMapping("save/demo")
    @ResponseBody
    public Map<String,Object> save(@RequestBody Demo demo) {
    	//参数{id:1,name:'name'}
    	System.out.println(demo);
    	
        return Map.of("success",true);
    }
    @PostMapping("demo/deleteByIds")
    @ResponseBody
    public Map<String,Object> deleteByIds(@RequestBody List<String>ids) {
    	//参数{id:1,name:'name'}
    	System.out.println(ids);
        return Map.of("success",true);
    }
    @PostMapping("demo/saveBatch")
    @ResponseBody
    public Map<String,Object> saveBatch(@RequestBody List<Demo> list) {
    	//参数{id:1,name:'name'}
    	System.out.println(list);
        return Map.of("success",true);
    }
    
    @GetMapping("j/list")
    public String jList(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
        model.addAttribute("list", getList("jsp"));
        return "jsp/index";
    }
    
    @GetMapping("j/pageList")
    public String pageList(Model model,Page<Test> testPage) {
        System.out.println("jsp pageList");
        List<Test> tests = IntStream.range(1, 101)
                .mapToObj(i -> new Test(i + ".name", i + ".tel"))
                .toList();
        testPage.setList(tests);
        model.addAttribute("page", testPage);
        model.addAttribute("msg", "hello pageList:" + LocalDateTime.now());
        return "jsp/pageList";
    }

    @GetMapping("t/index")
    public String tIndex(Model model) {
        System.out.println("thymeleaf index");
        model.addAttribute("msg", "hello thymeleaf:" + LocalDateTime.now());
        return "thymeleaf/index";
    }
    @GetMapping("t/list")
    public String tList(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello thymeleaf:" + LocalDateTime.now());
        model.addAttribute("list", getList("jsp"));
        return "thymeleaf/index1";
    }


    @GetMapping("f/index")
    public String fIndex(Model model) {
        System.out.println("freemarker index");
        model.addAttribute("msg", "hello freemarker:" + LocalDateTime.now());
        return "freemarker/index";
    }
    
    
    @GetMapping("f/list")
    public String fList(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello thymeleaf:" + LocalDateTime.now());
        model.addAttribute("list", getList("jsp"));
        return "freemarker/index";
    }
    
    public static Map<String,Demo> ryMap = new HashMap<>();
    static{
         Demo ry1=new Demo();
         ry1.setId("1");
         ry1.setName("张三");
         ry1.setSex("女");
         ry1.setAddress("海淀区西北旺镇大牛坊社区一期");
         ry1.setWork("市场部招生老师");
         Demo ry2=new Demo();
         ry2.setId("2");
         ry2.setName("李四");
         ry2.setSex("男");
         ry2.setAddress("海淀区西二旗宏福苑小区");
         ry2.setWork("市场部招生老师");
         ryMap.put(ry1.getId(), ry1);
         ryMap.put(ry2.getId(), ry2);
    }
    public static Map<String,List<Demo>> xyListMap = new HashMap<>();
    static{
    	 List<Demo> list=new ArrayList<>();
         Demo xy1=new Demo();
         xy1.setId("1");
         xy1.setName("学员1");
         xy1.setSex("女");
         xy1.setAddress("海淀区西北旺镇大牛坊社区一期");
         xy1.setWork("软工学院");
         xy1.setTeacherId("1");
         Demo xy2=new Demo();
         xy2.setId("2");
         xy2.setName("学员2");
         xy2.setSex("男");
         xy2.setAddress("海淀区西二旗宏福苑小区");
         xy2.setWork("基础学院");
         xy2.setTeacherId("1");
         list.add(xy1);
         list.add(xy2);
         Demo xy3=new Demo();
         xy3.setId("3");
         xy3.setName("学员3");
         xy3.setSex("男");
         xy3.setAddress("海淀区西二旗宏福苑小区");
         xy3.setWork("网工学院");
         xy3.setTeacherId("2");
         List<Demo> list2=new ArrayList<>();
         list2.add(xy3);
         xyListMap.put("1", list);
         xyListMap.put("2", list2);
    }
    @RequestMapping("t/ryList")
    @ResponseBody
    public Map<String,Object>  ryList() {
        return Map.of("code","0","data",ryMap.values(),"count",ryMap.size());
    }
    
    @RequestMapping("t/xyInfo")
    @ResponseBody
    public Map<String,Object>  xyInfo(@RequestParam(value="ryid", required=true) String ryid) {
        return Map.of("code","0","data",xyListMap.get(ryid),"count",xyListMap.get(ryid)==null?0:xyListMap.get(ryid));
    }
    
    @PostMapping("t/addRy")
    @ResponseBody
    public Map<String,Object> addRy(@RequestParam(value="name", required=false) String name
    		,@RequestParam(value="sex", required=false) String sex
    		,@RequestParam(value="address", required=false) String address
    		,@RequestParam(value="work", required=false) String work) {
    	Demo demo = new Demo();
    	demo.setId(String.valueOf(ryMap.size()+1));
    	demo.setName(name);
    	demo.setSex(sex);
    	demo.setAddress(address);
    	demo.setWork(work);
    	ryMap.put(demo.getId(),demo);
        return Map.of("success",true);
    }

    private List<Demo> getList(String name) {
	  return IntStream.range(1, 20).mapToObj(i -> new Demo(i+"", name + "_name_" + i)).toList();
	}
    public static Map<String,List<School>> schoolMap = new HashMap<>();
    static{
    	School school=new School("1","北京校区1");
        School school2=new School("2","北京校区2");
        List<School> list=new ArrayList<>();
        list.add(school2);
        list.add(school);
        School school3=new School("3","上海校区1");
        School school4=new School("4","上海校区2");
        List<School> list2=new ArrayList<>();
        list2.add(school3);
        list2.add(school4);
        School school5=new School("5","江苏校区1");
        School school6=new School("6","江苏校区2");
        List<School> list3=new ArrayList<>();
        list3.add(school5);
        list3.add(school6);
        schoolMap.put("1", list);
        schoolMap.put("2", list2);
        schoolMap.put("3", list3);
    }
    
    
    @RequestMapping("t/getShool")
    public String getShool(City city, Model model) {
        model.addAttribute("dataList", schoolMap.get(city.getCity()));
        return "thymeleaf/data";
    }
    public static Map<String,List<Department>> departmentMap = new HashMap<>();
    static{
    	Department department=new Department("1","北京市场部");
        Department department2=new Department("2","北京市场部");
        List<Department> list=new ArrayList<>();
        list.add(department);
        list.add(department2);
        Department department3=new Department("3","北京市场部");
        Department department4=new Department("4","北京市场部");
        List<Department> list2=new ArrayList<>();
        list2.add(department3);
        list2.add(department4);
        Department department5=new Department("5","江苏市场部");
        Department department6=new Department("6","江苏市场部");
        List<Department> list3=new ArrayList<>();
        list3.add(department5);
        list3.add(department6);
        departmentMap.put("1", list);
        departmentMap.put("2", list2);
        departmentMap.put("3", list3);
    }
    @RequestMapping("f/getDepartment")
    public String getDepartment(School school, Model model) {
    	model.addAttribute("dataList", departmentMap.get(school.getSchool()));
        return "freemarker/data";
    }
}
